import router from "."
import store from "../store"
import {
  Message
} from "element-ui"
import {
  getToken,
  setToken
  // removeToken
} from "@/utils/auth"

const whiteList = ["/login", "/sso/logout"]

router.beforeEach((to, from, next) => {
  switch (to.path) {
    case "/task/monitoringOffLine":
      switch (to.query.type.toString()) {
        case "1":
          to.meta.title = "离线数据采集监控"
          break
        case "3":
          to.meta.title = "接口采集监控"
          break
        case "4":
          to.meta.title = "文件服务器采集监控"
          break
        case "5":
          to.meta.title = "网络爬虫采集监控"
          break
      }
      next()
      break
    default:
      next()
      break
  }
})
router.beforeEach((to, from, next) => {
  let search = window.location.search.split("?")
  if (search.length === 2) {
    let arr = search[1].split('&')
    if (arr.some(a => a.split('=')[0] === 'token')) {
      const token = 'Bearer ' + arr.filter(a => a.split('=')[0] === 'token')[0].split('=')[1]
      store.commit('SET_TOKEN', token)
      setToken(token)
    }
  }
  // 路由跳转统一处理
  // if (!getToken()) {
  //   store.commit("SET_TOKEN", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxLGFkbWluIiwiaXNzIjoiYWRtaW4iLCJleHAiOjE2MTcxNjc5MjksImlhdCI6MTYxNjU2MzEyOSwicm9sIjoiUk9MRV9BRE1JTiJ9.akP4Z3ZGjyTeg3aigJKasIz_Cy_c50V4bdhRnEKNbmb3QTmImK0_m2kxkH2hvo5ekOkQnfsNtTdOTSkftxehlg")
  //   setToken("Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxLGFkbWluIiwiaXNzIjoiYWRtaW4iLCJleHAiOjE2MTcxNjc5MjksImlhdCI6MTYxNjU2MzEyOSwicm9sIjoiUk9MRV9BRE1JTiJ9.akP4Z3ZGjyTeg3aigJKasIz_Cy_c50V4bdhRnEKNbmb3QTmImK0_m2kxkH2hvo5ekOkQnfsNtTdOTSkftxehlg")
  // }
  // localStorage.setItem("testId", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxLGFkbWluIiwiaXNzIjoiYWRtaW4iLCJleHAiOjE2MTcxNjc5MjksImlhdCI6MTYxNjU2MzEyOSwicm9sIjoiUk9MRV9BRE1JTiJ9.akP4Z3ZGjyTeg3aigJKasIz_Cy_c50V4bdhRnEKNbmb3QTmImK0_m2kxkH2hvo5ekOkQnfsNtTdOTSkftxehlg");

  next()
  // if (getToken()) {
  //   // 走这里判断切换子系统
  //   if (to.path === "/login") {
  //     next({
  //       path: "/"
  //     });
  //   } else if (to.path === "/sso/logout") {
  //     next()
  //   } else if (store.getters.navBar.length === 0) {
  //     store.dispatch("GetUserInfo").then(res => { // dispatch 触发获取当前用户信息
  //       store.dispatch("GetNav", getToken()).then((res) => { // dispatch 触发获取目录栏
  //         if (window.location.search) {
  //           window.location.href = window.location.href.replace( // path跳转
  //             window.location.search,
  //             ""
  //           );
  //         }
  //         next();
  //       })
  //     }).catch(() => {
  //       store.dispatch("LogOut").then(() => {
  //         Message.error("验证失败, 请重新登录")
  //         next({
  //           path: "/login"
  //         })
  //       })
  //     });
  //   } else {
  //     next()
  //   }
  // } else {
  //   let search = window.location.search.split("?");
  //   if (whiteList.indexOf(to.path) !== -1) {
  //     next()
  //   } else if (search.length === 2) {
  //     let obj = {};
  //     let arr = search[1].split("&");
  //     // 统一登录-code登录
  //     if (arr.some(a => a.split("=")[0] === "code")) {
  //       obj.code = arr.filter(a => a.split("=")[0] === "code")[0].split("=")[1];
  //       store.dispatch("loginByCode", obj)
  //         .then(res => {
  //           router.push({
  //             path: "/"
  //           });
  //         })
  //         .catch(() => {
  //           Message.error("登录失败");
  //         });
  //     } else if (arr.some(a => a.split("=")[0] === "username")) {
  //       // 统一登录-用户名密码登录
  //       obj.username = arr.filter(a => a.split("=")[0] === "username")[0].split("=")[1];
  //       obj.password = arr.filter(a => a.split("=")[0] === "password")[0].split("=")[1];
  //       store.dispatch("loginByUsername", obj)
  //         .then(res => {
  //           router.push({
  //             path: "/"
  //           });
  //         })
  //         .catch(() => {
  //           Message.error("登录失败");
  //         });
  //     }
  //   } else {
  //     next("/login")
  //   }
  // }
})
